<template>
    <d2-container :filename="filename">
        <template slot="header">
            <el-form :inline="true" :model="queryParam" class="demo-form-inline">
                <el-form-item label="数据库名称">
                    <el-select v-model="queryParam.dbName"
                               @change="getList"
                               placeholder="请选择">
                        <el-option
                                v-for="item in dbNameOptions"
                                :key="item.TABLE_SCHEMA"
                                :label="item.TABLE_SCHEMA"
                                :value="item.TABLE_SCHEMA">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button size="mini"
                               type="success"
                               @click="dialogFormVisible = true"
                               icon="el-icon-edit-outline">按模块生成
                    </el-button>
                </el-form-item>
            </el-form>
        </template>
        <el-table v-loading="listLoading" :data="tables" border fit stripe highlight-current-row style="width: 100%;">
            <el-table-column label="数据库名" min-width="180" align="center">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tableName }}</span>
                </template>
            </el-table-column>

            <el-table-column label="备注信息" width="180" align="center">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tableComment }}</span>
                </template>
            </el-table-column>

            <el-table-column label="引擎类型" width="180" align="center">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.ENGINE }}</span>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" min-width="180" align="center">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0"
                    :total="total"
                    :page.sync="queryParam.current"
                    :limit.sync="queryParam.size"
                    @pagination="getList"/>
        <!--对话框-->
        <el-dialog title="模块代码参数配置" :visible.sync="dialogFormVisible">
            <el-form :model="generatorOption">
                <el-form-item label="工程路径" align="left" :label-width="labelWidth">
                    <el-input v-model="generatorOption.projectPath" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="作者名称" :label-width="labelWidth">
                    <el-input v-model="generatorOption.authorName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="jdbc链接" :label-width="labelWidth">
                    <el-input v-model="generatorOption.dbUrl" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="驱动名称" :label-width="labelWidth">
                    <el-input v-model="generatorOption.driverName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据库账号" :label-width="labelWidth">
                    <el-input v-model="generatorOption.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据库密码" :label-width="labelWidth">
                    <el-input v-model="generatorOption.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="父包名" :label-width="labelWidth">
                    <el-input v-model="generatorOption.parentName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="模块名" :label-width="labelWidth">
                    <el-input v-model="generatorOption.moduleName" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary"
                           v-loading.fullscreen.lock="fullScreenLoading"
                           @click="doExecuteGenerate">确 定
                </el-button>
            </div>
        </el-dialog>

    </d2-container>
</template>

<script>
  import { generateByModule, getDbOptions, pageTable } from '@api/sysadmin/codegen'
  import pagination from '@/components/Pagination'

  export default {
    name: 'codegen',
    components: {
      pagination
    },
    data () {
      return {
        filename: __filename,
        listLoading: true,
        tables: [],
        total: 0,
        labelWidth: '120px',
        dbNameOptions: [],
        dialogFormVisible: false,
        fullScreenLoading: false,
        generatorOption: {
          projectPath: 'G:\\px\\maynrent-cloudx\\maynrent-provider\\maynrent-order',
          authorName: '@xsomnus666_xiawenye★',
          dbName: 'maynrent-order',
          dbUrl: 'jdbc:mysql://192.168.10.110:4306/maynrent-order?useSSL=false&serverTimezone=Asia/Shanghai',
          driverName: 'com.mysql.cj.jdbc.Driver',
          username: 'root',
          password: '123456',
          moduleName: 'order',
          parentName: 'com.xsomnus.maynrent.provider'
        },
        queryParam: {
          current: 1,
          size: 10,
          dbName: undefined,
          tableName: undefined
        }
      }
    },
    created () {
      this.getList()
      this.initDbOptions()
    },
    methods: {
      initDbOptions () {
        getDbOptions().then(res => {
          this.dbNameOptions = res
        })
      },
      getList () {
        pageTable(this.queryParam).then(res => {
          this.tables = res.records
          this.total = parseInt(res.total)
          this.listLoading = false
        })
      },
      doExecuteGenerate () {
        this.fullScreenLoading = true
        this.generatorOption.dbName = this.queryParam.dbName
        generateByModule(this.queryParam.dbName, this.generatorOption).then(res => {
          this.fullScreenLoading = false
          if (res) {
            this.$message({
              message: '恭喜你，' + this.queryParam.dbName + '模块代码生成成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '非常遗憾，' + this.queryParam.dbName + '模块代码生成失败',
              type: 'error'
            })
          }
        }).catch(() => {
          this.fullScreenLoading = false
        })
      }
    }
  }
</script>
